package com.winderinfo.yikaotianxia.ui.order;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class WholeFragment extends BaseFragment implements Contract.View {
    @BindView(R.id.b_recycle)
    RecyclerView bRecycle;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_banxin;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(getActivity());
        bRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {
        //获取用户Id
        String userID = spUtils.getString("UserID", "");
        if (userID != null) {
            /**
             * @id 用户Id
             * */
            map.clear();
            map.put("id", 1);
            presenter.onPostRequest(Api.DINGDAN, map, OrderBean.class);
        } else {
            ToastUtil.showSuccess(getActivity(), "请先登录");
        }
    }

    @Override
    public void onSuccess(Object data) {
        //订单数据
        if (data instanceof OrderBean) {
            List<OrderBean.YkOrderListBean> ykOrderList = ((OrderBean) data).getYkOrderList();
            OrderAdapter orderAdapter = new OrderAdapter(getContext(), ykOrderList);
            bRecycle.setAdapter(orderAdapter);
            //确认收货
            orderAdapter.setOnShouHuo(new OrderAdapter.OnShouHuo() {
                @Override
                public void getId(int orderId) {
                    //请求确认收货接口
                    /**
                     * @id 订单Id
                     * */
                    map.clear();
                    map.put("id", orderId);
                    presenter.onPostRequest(Api.SHOUHUO, map, RegisterBean.class);
                }
            });
            //查看订单
            orderAdapter.setOnDetails(new OrderAdapter.OnDetails() {
                @Override
                public void getOrederId(int orderID) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("orderID", orderID);
                    openActivity(OrderDetailsActivity.class, bundle);
                }
            });
        }
        //确认收货
        if (data instanceof RegisterBean) {
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(getActivity(), ((RegisterBean) data).getMsg());
            } else {
                ToastUtil.showSuccess(getActivity(), ((RegisterBean) data).getMsg());
            }
        }

    }

    @Override
    public void onFailure(Object error) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
